<template>
	<u-popup v-model="show" border-radius="16" mode="bottom">
		<view class="choose-pop-content">
			<view class="header-wrap">
				<text class="header-title">请选择邀请设备</text>
			</view>
			<scroll-view scroll-y="true" class="list-scroll" @scrolltolower="initData()">
				<view class="device-record-item" v-for="(item,index) in list" :key="index"
					@click="handleCheckItem(item)">
					<view class="checkbox-item" :class="[item.id === activeId ? 'active' : '']">
						<u-icon color="#ffffff" size="30rpx" name="checkmark" />
					</view>
					<view class="item-info-wrap">
						<view class="item-title">
							<text>{{ item.shebeixinxi.xinghao }}</text>
						</view>
						<view class="item-code">
							<text>设备编码：</text>
							<text class="color-danger">{{ item.shebeixinxi.bianma }}</text>
						</view>
					</view>
				</view>

			</scroll-view>
			<view class="footer-wrap">
				<u-button style="flex: 1;" shape="circle" @click="handleClose">取消</u-button>
				<u-button style="flex: 1;" shape="circle" type="primary" @click="handleConfirm">确定</u-button>
			</view>
		</view>
	</u-popup>
</template>

<script>
	import { request, showLoading, toast } from "@/utils/tool.js"
	export default {
		data() {
			return {
				show: false,
				list: [],
				hasMore: true,
				page: 1,
				activeId: "",
				name: ""
			}
		},
		emits: ["success"],
		methods: {
			handleConfirm() {
				const { activeId, name } = this
				if (!activeId.toString()) return toast({ title: "请选择" })
				this.$emit("success", {
					name,
					id: activeId
				})
				this.show = false
			},
			handleCheckItem(rowItem) {
				this.activeId = rowItem.id
				this.name = rowItem.shebeixinxi.xinghao
			},
			async initData() {
				if (!this.hasMore) return
				const { page } = this
				const resp = await request({
					url: "/tihuo/guanjiashebeilist",
					method: "GET",
					data: {
						page_no: page,
						page_size: 20
					}
				})
				if (page === 1) this.list = []
				const { lists, count, page_no, page_size } = resp.data
				this.list.push(...lists)
				this.hasMore = page_no * page_size < count
				if (this.hasMore) {
					this.page = +1
				}
				console.log(resp, 'resp');
			},
			open(activeId, name) {
				this.page = 1
				this.hasMore = true
				this.list = []
				this.show = true
				this.activeId = activeId
				this.name = name
				this.initData()
			},
			handleClose() {
				this.show = false
			}
		},
	}
</script>

<style lang="scss" scoped>
	.choose-pop-content {
		padding: 32rpx 46rpx 20rpx;
		box-sizing: border-box;

		.list-scroll {
			width: 100%;
			height: 660rpx;

			.device-record-item {
				display: flex;
				align-items: center;
				border-bottom: 2rpx solid #F4F4F4;
				padding-bottom: 16rpx;
				margin-bottom: 20rpx;

				&:last-child {
					margin-bottom: 0;
				}

				.checkbox-item {
					width: 44rpx;
					height: 44rpx;
					flex-shrink: 0;
					border: 2rpx solid #809CF6;
					border-radius: 50%;
					overflow: hidden;
					display: flex;
					align-items: center;
					justify-content: center;
					flex-shrink: 0;

					&.active {
						background-color: #3361F0;
						border-color: #3361F0;
					}
				}

				.item-info-wrap {
					margin-left: 28rpx;

					.item-code {
						font-size: 30rpx;
						color: #575757;
						line-height: 40rpx;
						margin-top: 8rpx;

					}

					.item-title {
						font-size: 30rpx;
						color: #575757;
						line-height: 40rpx;
					}
				}
			}
		}

		.footer-wrap {
			display: flex;
			align-items: center;
			padding: 16rpx;

			::v-deep {

				uni-button,
				button {
					flex: 1;

					&:first-child {
						margin-right: 32rpx;
					}

				}
			}
		}

		.header-wrap {
			display: flex;
			align-items: center;
			justify-content: center;
			position: relative;
			margin-bottom: 30rpx;

		}

		.header-title {
			font-weight: bold;
			font-size: 32rpx;
			color: #000000;
			line-height: 44rpx;
			text-align: center;
		}
	}
</style>